package com.forsir.android.mhfinance.storeObjects;

import android.content.ContentValues;
import android.database.Cursor;

import com.forsir.android.mhfinance.helper.DbAdapter;

public class Category extends IStorable {
	private String mName;
	private SubcategoryList mSubcategoryList = new SubcategoryList();

	public Category() {
	}

	public Category(int id) {
		setId(id);
	}

	public Category(Cursor cursor) {
		fillFromCursor(cursor);
	}

	public Category(String name) {
		mName = name;
	}

	public void setName(String name) {
		mName = name;
	}

	@Override
	public String getName() {
		return mName;
	}

	public void setSubcategoryList(SubcategoryList subcategoryList) {
		mSubcategoryList = subcategoryList;
	}

	public SubcategoryList getSubcategoryList() {
		return mSubcategoryList;
	}

	@Override
	public ContentValues getContent() {
		final ContentValues content = new ContentValues();
		content.put(DbAdapter.CATEGORY_NAME, getName());
		// content.put(DbAdapter.CATEGORY_PARENT, null);
		return content;
	}

	@Override
	public String getTableName() {
		return DbAdapter.CATEGORY_TABLE;
	}

	@Override
	public String[] getColumns() {
		return new String[] { DbAdapter.ID, DbAdapter.CATEGORY_NAME };
	}

	@Override
	/*
	 * * gets values from actual position of cursor
	 */
	public IStorable fillFromCursor(Cursor cursor) {
		if (cursor == null) {
			setId(-1);
			mName = "";
			return this;
		}

		if (!cursor.isAfterLast()) {
			setId(cursor.getLong(cursor.getColumnIndex(DbAdapter.ID)));
			mName = cursor.getString(cursor.getColumnIndex(DbAdapter.CATEGORY_NAME));
		}

		return this;
	}

	public int getSumCount() {
		return mSubcategoryList.getSumCount();
	}

	public float getSumWithdraw() {
		return mSubcategoryList.getSumWithdraw();
	}

	public float getSumDeposit() {
		return mSubcategoryList.getSumDeposit();
	}

	public void loadSubcategories(DbAdapter dbHelper) {
		mSubcategoryList.loadAll(dbHelper, getId());
	}
}
